﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Warehouse.API.Applcation.Command.InventoryCommand;
using Warehouse.Domain;
using Warehouse.Infrstructre;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.InventoryCommandHandler
{
    /// <summary>
    /// 修改仓库盘点中介者
    /// </summary>
    public class UpdInventoryCommandHandler:IRequestHandler<UpdInventoryCommand,int>
    {
        private readonly IMapper _mapper;
        private readonly IInventoryRepository _repoRep;
        private readonly IInventProductRepository _productRep;
        private readonly EFCoreDBcontext DB;

        public UpdInventoryCommandHandler(IMapper mapper, IInventoryRepository repoRep, IInventProductRepository productRep, EFCoreDBcontext dB)
        {
            _mapper = mapper;
            _repoRep = repoRep;
            _productRep = productRep;
            DB = dB;
        }

        public async Task<int> Handle(UpdInventoryCommand request, CancellationToken cancellationToken)
        {
            using (var transaction = DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //修改盘点表
                    var s = _mapper.Map<Inventory>(request.updDto);
                    await _repoRep.UpdateAsync(s);
                    //查询盘点产品表里的某盘点的所有数据
                    var res = (await _productRep.GetAllAsyns()).Where(x => x.InventoryId == s.InventoryId);
                    //删除产品盘点里的某个盘点的所有数据
                    foreach (var item in res)
                    {
                        await _productRep.RemoveAsync(item);
                    }
                    //修改盘点产品表（添加重新赋值）
                    foreach (var item in request.updDto.updInventGuan)
                    {
                        InventProduct product = new InventProduct()
                        {
                            //InventProductIdId = item.InventProductIdId,
                            InventoryId = s.InventoryId,
                            ProductId = item.ProductId,
                            ProductName = item.ProductName,
                            Number=item.Number,
                            InventoryNumber=item.InventoryNumber,
                            ProductCode = item.ProductCode,
                            Specifications = item.Specifications,
                            Batch = item.Batch,
                            Unit = item.Unit,
                            WarehouseLocation = item.WarehouseLocation,
                            office_id = item.office_id,
                            user_id = item.user_id,
                            AddName = item.AddName,
                            AddTime = item.AddTime
                        };
                        await _productRep.AddAsync(product);
                    }
                    //提交事务
                    transaction.Commit();
                    return 1;
                }
                catch (Exception)
                {
                    //回滚事务
                    transaction.Rollback();
                    throw;
                }
            }
        }
    }
}
